<?php
/**
 * Created by PhpStorm.
 * User: robot
 * Date: 2018-04-26
 * Time: 01:12
 */

namespace app\pub\controller;


use app\base\model\User;
use app\base\validate\PubValidate;
use think\Controller;
use think\Request;

class Register extends Controller
{

    /***
     * 注册页面
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $request = Request::instance();
        if ($request->isAjax()) {

            $data = $request->param();
            $validate = new PubValidate();
            if (!$validate->check($data)) {
                return ['code'=>-1,'msg'=>$validate->getError()];
            }
            $user = User::get(['user_name' => $data['user_name']]);
            if (!is_null($user)) {
                return ['code'=>-1,'msg'=>'账号已存在'];
            }
            $adduser=User::create([
                'user_name' => $data['user_name'],
                'user_pwd' => md5($data['user_pwd']),
                'user_nick' => $data['user_nick'],
            ]);
            if ($adduser) {
                return ['code'=>1,'msg'=>'注册成功'];
            } else {
                return ['code'=>-1,'msg'=>'注册失败'];
            }
        }
        return $this->fetch();
    }

}